import Vue from 'vue'
import JSEncrypt from 'jsencrypt'
import { getPublicKey } from '~/api/encrypt'
async function encrypt(str) {
  const encryptor = new JSEncrypt()
  let newstr = ''
  // 获取公钥
  await getPublicKey().then(async(res) => {
    // 把公钥放到加密对象中
    encryptor.setPublicKey(res.data)
    // 返回一个加密后的字符串
    newstr = encryptor.encrypt(str)
  })

  return newstr
}

Vue.prototype.$encrypt = encrypt
